<template>
  <el-card>
    <div slot="header" shadow="hover">
      <div class="info-completion-container">
        <el-form
          :model="ruleForm"
          ref="ruleFormRef"
          label-width="120px"
          class="center-form"
        >
          <h1 class="form-title">企业经营信息提交</h1>
          <el-col :span="12">
            <!-- 注册资本（单位：万元） -->
            <el-form-item label="注册资本" class="form-item-spaced" :rules="[{ required: true, message: '请输入注册资本', trigger: 'blur' }]">
              <el-input v-model="ruleForm.registermoney">
                <template #append>万元</template>
              </el-input>
            </el-form-item>
            <!-- 成立时间 -->
            <el-form-item label="成立时间" class="form-item-spaced" :rules="[{ required: true, message: '请选择成立时间', trigger: 'change' }]">
              <el-date-picker
                v-model="ruleForm.finishtime"
                type="date"
                placeholder="选择日期"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
            <!-- 年度营收 -->
            <el-form-item label="年度营收" class="form-item-spaced" :rules="[{ required: true, message: '请输入年度营收', trigger: 'blur' }]">
              <el-input v-model="ruleForm.annualRevenue">
                <template #append>万元</template>
              </el-input>
            </el-form-item>
            <!-- 年度税收 -->
            <el-form-item label="年度税收" class="form-item-spaced" :rules="[{ required: true, message: '请输入年度税收', trigger: 'blur' }]">
              <el-input v-model="ruleForm.annualTax">
                <template #append>万元</template>
              </el-input>
            </el-form-item>
            <!-- 年度利润 -->
            <el-form-item label="年度利润" class="form-item-spaced" :rules="[{ required: true, message: '请输入年度利润', trigger: 'blur' }]">
              <el-input v-model="ruleForm.annualProfit">
                <template #append>万元</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-form-item class="form-actions">
            <div class="button-container">
              <el-button type="primary" size="large" @click="submitCompletion" :loading="loading">保存修改</el-button>
              <el-button type="primary" size="large" @click="goToRental">继续完善公司租赁信息</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'

// 表单引用
const ruleFormRef = ref(null)
const route = useRoute()
const router = useRouter()
const loading = ref(false)

// 接收路由参数中的companyId
const companyId = ref('')
const companyName = ref('')

// 表单数据
const ruleForm = ref({
  registermoney: "",
  finishtime: "",
  annualRevenue: "",
  annualTax: "",
  annualProfit: ""
})

onMounted(() => {
  // 获取路由中的参数
  companyId.value = route.query.companyId
  companyName.value = route.query.companyName
  
  if (!companyId.value) {
    ElMessage.error('未获取到公司信息，无法提交表单')
  }
})

// 跳转租赁信息页面（携带参数）
const goToRental = () => {
  if (!companyId.value) {
    ElMessage.error('未获取到公司ID，无法跳转')
    return
  }
  
  router.push({ 
    name: 'enterpriserentalsubmission',
    query: { 
      companyId: companyId.value, 
      companyName: companyName.value 
    }
  })
}

// 提交表单
const submitCompletion = async () => {
  if (!ruleFormRef.value) return
  ruleFormRef.value.validate(async (valid) => {
    if (!valid) return
    if (!companyId.value) {
      ElMessage.error('公司ID不存在，请重新进入页面')
      return
    }

    try {
      loading.value = true
      // 构造提交数据
      const submitData = {
        companyId: companyId.value,
        registerCapital: ruleForm.value.registermoney + "万元",
        establishDate: ruleForm.value.finishtime ? ruleForm.value.finishtime.toISOString().split('T')[0] : "",
        annualRevenue: ruleForm.value.annualRevenue + "万元",
        annualTax: ruleForm.value.annualTax + "万元",
        annualProfit: ruleForm.value.annualProfit + "万元"
      }

      const response = await axios.post(
      `${import.meta.env.VITE_API_BASE_URL}/api/company/submitOtherInfo`, 
      submitData
     )

      if (response.data.code === 200) {
        ElMessage.success('企业信息提交成功！')
      } else {
        ElMessage.error(response.data.message || '提交失败，请重试')
      }
    } catch (error) {
      console.error('提交接口调用失败：', error)
      ElMessage.error('网络错误，提交失败，请稍后再试')
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.form-item-spaced {
  margin-bottom: 30px !important;
}

.button-container {
  display: flex;
  justify-content: center;
  width: 100%;
  gap: 15px; /* 增加按钮间距 */
}

.el-form .form-actions {
  display: flex;
  justify-content: center;
  margin-top: 25px;
  margin-bottom: 10px;
  width: 100%;
}

.form-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
}
</style>
